﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ApplProtocol;
using System.Linq;

public class MemberShip
{
    private string userName;
    private bool isReady = false;
    private bool isNull = true;

    public bool IsNull
    {
        get { return isNull; }
    }

    public string UserName
    {
        get { return userName; }
    }
    public bool IsReady
    {
        get { return isReady; }
    }

    public MemberShip(string name, bool isready)
    {
        userName = name;
        this.isReady = isready;
        isNull = name == "null"; 
    }

    public MemberShip()
    {
        isNull = true;
    }

    public override string ToString()
    {
        return userName;
    }
}

public class Room
{
    private int owner = 0;
    private int memberCnt = 0;

    private GameObject roomUIElement;
    private MemberShip[] members = new MemberShip[2];

    public bool Playing
    {
        get { return Members[0].IsReady && Members[0].IsReady; }
    }

    public bool ExistLeftPlayer
    {
        get { return !Members[0].IsNull; }
    }

    public bool ExistRightPlayer
    {
        get { return !Members[1].IsNull; }
    }

    public string LeftPlayerName
    {
        get { return Members[0].IsNull ? "" : Members[0].UserName; }
    }

    public string RightPlayerName
    {
        get { return Members[1].IsNull ? "" : Members[1].UserName; }
    }

    public bool LeftPlayerReady
    {
        get { return Members[0].IsReady; }
    }

    public bool RightPlayerReady
    {
        get { return Members[1].IsReady; }
    }

    public string OwnerName
    {
        get { return members[owner].UserName; }
    }

    public MemberShip[] Members
    {
        get { return members; }
    }

    public bool IsFull
    {
        get { return ExistLeftPlayer && ExistRightPlayer; }
    }

    public Room(MemberShip[] memberShips, int owner, Transform roomListPanel)
    {
        this.owner = owner;
        members = memberShips;

        roomUIElement = UnityEngine.Object.Instantiate(
            Resources.Load<GameObject>("Prefabs/UI/Room"), roomListPanel);
        
        roomUIElement.transform.Find("owner").GetComponent<Text>().text = "房主：" + members[owner];

        Debug.Log(members[owner].UserName);

        //RoomUIElement.transform.Find("owner").GetComponent<Text>().text = "房主：";
        //Debug.Log(roomUIElement);

        //memberCnt = (members[0].ToString() == "null" ? 1 : 0) +
        //    (members[1].ToString() == "null" ? 1 : 0);
    }


    public void Bind(RoomSceneUI uiCallback)
    {
        roomUIElement.GetComponent<Button>().onClick.AddListener(
            delegate ()
            {
                uiCallback.BtnRoomDetailOnCliked(this);
            }
        );
        //Debug.Log("Bind");
    }

    public void Close()
    {
        UnityEngine.Object.Destroy(roomUIElement);
    }
}



